﻿activityFeed.UpdateViewModel = function (currenctUser, removeUpdateHandler, update) {
    var self = this;

    this.id = ko.observable();
    this.contentType = ko.observable();
    this.content = ko.observable();
    this.publisher = ko.observable();
    this.source = ko.observable();
    this.publishedOn = ko.observable(new Date());

    this.update = update;

    this.isEditable = ko.computed(function () {
        return !!self.id() && !!self.publisher() && self.publisher().id == currenctUser.id;
    });

    this.removeUpdate = ko.asyncCommand(
      function () {
          return removeUpdateHandler(self);
      }, null, self.isEditable);



    // comments
    this.comments = ko.observableArray();
    this.hasComments = ko.computed(function () {
        return self.comments.length > 0;
    });

    this.newCommentText = ko.observable();

    this.postNewComment = ko.asyncCommand(
        function () {
            var promise = activityFeed.api.postComment(self.id(), self.newCommentText()).pipe(function (result) {
                var mapper = new activityFeed.UpdatesMapper(currenctUser);
                var comment = mapper.mapCommentToCommentViewModel(result.value, self);
                self.comments.push(comment);

                self.newCommentText("");

                return comment;
            });

            return promise;
        }, self.newCommentText);

    this.removeCommentHandler = function (comment) {
        return activityFeed.api.removeComment(self.id(), comment.id()).pipe(function () {
            self.comments.remove(comment);
        });
    };

    this.isCommentBoxVisible = ko.observable(false);

    this.showHideComments = function () {
        self.isCommentBoxVisible(!self.isCommentBoxVisible());
    };




    // likes
    this.likes = ko.observableArray();

    var getCurrentUserLike = function () {
        var length = self.likes().length;
        for (var i = 0; i < length; i++) {
            var user = self.likes()[i].user();
            if (user && user.id == currenctUser.id) {
                return self.likes()[i];
            }
        }
        return null;
    };

    this.hasLikes = ko.computed(function () { return !!self.likes().length; }, this);

    this.likesExcludingMine = ko.computed(function () {
        var result = new Array();
        for (var i = 0, length = self.likes().length; i < length; i++) {
            var like = self.likes()[i];
            if (like.user() && currenctUser.id != like.user().id) {
                result.push(like);
            }
        }
        return result;
    }, this);

    this.likedByMe = ko.computed(function () {
        var currentUserLike = getCurrentUserLike();
        return !!currentUserLike;
    }, this);

    this.toggleLike = ko.asyncCommand(
        function () {
            if (self.likedByMe()) {
                return removeLike();
            } else {
                return addLike();
            }
        }, self.id);

    var addLike = function () {
        var promise = activityFeed.api.likeUpdate(self.id()).pipe(function (result) {
            var mapper = new activityFeed.UpdatesMapper(currenctUser);
            var like = mapper.mapLikeToLikeViewModel(result.value);
            self.likes.push(like);
        });

        return promise;
    };

    var removeLike = function () {
        var promise = activityFeed.api.removeLikeFromUpdate(self.id()).pipe(function () {
            var currentUserLike = getCurrentUserLike();
            self.likes.remove(currentUserLike);
        });

        return promise;
    };
};
